package com.hks360.car_treasure.manager;

import android.content.Context;
import android.telecom.Call;

import com.google.gson.JsonObject;
import com.hks360.car_treasure.R;
import com.hks360.car_treasure.common.NetConstant;
import com.hks360.car_treasure.common.RequestKey;
import com.hks360.car_treasure.util.LogUtil;
import com.hks360.des2.Des2;
import com.hks360.library.nohttp.CallServer;
import com.hks360.library.nohttp.HttpCallback;
import com.hks360.library.util.DateUtil;
import com.hks360.nohttp.JsonArrayRequest;
import com.hks360.nohttp.Request;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.MessageFormat;
import java.util.Map;

/**
 * Created by xiaote on 2016/3/15.
 */
public class CommonDataManager {
    private static CommonDataManager instance;

    private Context context;

    private CommonDataManager(Context context) {
        this.context = context;
    }

    public static synchronized CommonDataManager getInstance(Context context) {
        if (instance == null) {
            instance = new CommonDataManager(context);
        }
        return instance;
    }

    public void userLog(int what, String mobile, String password, HttpCallback<String> callback) {
        String actionName = "UserLog";
        JSONObject obj = new JSONObject();
        try {
            obj.put(RequestKey.PHONE, mobile);
            obj.put(RequestKey.PASSWORD, password);
        } catch (Exception e) {

        }
        CallServer.getRequestInstance().post(context, NetConstant.getRequestUrl(actionName), what, obj.toString(),
                callback);
    }

    //用户注册的而网络接口
    public void userRegister(int what, String phone, String password, String code, HttpCallback<String> callback) {
        String actionName = "UserSign";
        JSONObject obj = new JSONObject();
        try {
            obj.put(RequestKey.PHONE, phone);
            obj.put(RequestKey.PASSWORD, password);
            obj.put(RequestKey.CODE, code);
            obj.put(RequestKey.TRUENAME, "");
        } catch (Exception e) {

        }
        CallServer.getRequestInstance().post(context, NetConstant.getRequestUrl(actionName), what, obj.toString(),
                callback);
    }

    //用户发送验证码的网络接口
    public void sendCheckCode(int what, String phone, HttpCallback<String> callback) {
        String actionName = "SendCheckCode";
        JSONObject obj = new JSONObject();
        try {
            obj.put(RequestKey.PHONE, phone);
        } catch (Exception e) {

        }
        CallServer.getRequestInstance().post(context, NetConstant.getRequestUrl(actionName), what, obj.toString(),
                callback);
    }

    //用户修改密码的网络接口
    public void userChangePasswordCheckCode(int what, String phone, String checkcode, String password,
                                            HttpCallback<String> callback) {
        String actionName = "UserChangePasswordCheckCode";
        JSONObject obj = new JSONObject();
        try {
            obj.put(RequestKey.PHONE, phone);
            obj.put(RequestKey.CODE, checkcode);
            obj.put(RequestKey.NEW_PASSWORD, password);
        } catch (Exception e) {

        }
        CallServer.getRequestInstance().post(context, NetConstant.getRequestUrl(actionName), what, obj.toString(),
                callback);
    }

    //用户绑定设备的网络接口
    public void bindUser(int what, String phone, String vin, String tboxid, HttpCallback<String> callback) {
        String actionName = "BindUser";
        JSONObject obj = new JSONObject();
        try {
            obj.put(RequestKey.PHONE, phone);
            obj.put(RequestKey.VIN, vin);
            obj.put(RequestKey.TBOX_ID, tboxid);
        } catch (Exception e) {

        }
        CallServer.getRequestInstance().post(context, NetConstant.getRequestUrl(actionName), what, obj.toString(),
                callback);
    }

    //获取设备状态接口
    public void getCurrentState(int what, String tboxid, HttpCallback<String> callback) {
        String actionName = "GetCurrentState";
        JSONObject obj = new JSONObject();
        try {
            obj.put(RequestKey.TBOX_ID, tboxid);
        } catch (Exception e) {

        }
        CallServer.getRequestInstance().post(context, NetConstant.getRequestUrl(actionName), what, obj.toString(),
                callback);
    }

    //发送控制车辆指令
    public void sendCommand(int what, String tboxid, String command, HttpCallback<String> callback) {
        String actionName = "SendCommand";
        JSONObject obj = new JSONObject();
        try {
            obj.put(RequestKey.TBOX_ID, tboxid);
            obj.put(RequestKey.COMMAND, command);
        } catch (Exception e) {

        }
        CallServer.getRequestInstance().post(context, NetConstant.getRequestUrl(actionName), what, obj.toString(),
                callback);
    }

    //获取当前车辆的状态
    public void readCurrentLoaction(int what,String tboxid,HttpCallback<String> callback) {
        String actionName = "ReadCurrentLocation";
        JSONObject obj = new JSONObject();
        try{
            obj.put(RequestKey.TBOX_ID,tboxid);
        }catch (Exception e) {

        }
        LogUtil.e("test",obj.toString());
        CallServer.getRequestInstance().post(context,NetConstant.getRequestUrl(actionName),what,obj.toString(),callback);
    }

    //获取行程
    public void readJourneyLoaction(int what,String tboxid, String startTime,String endTime, HttpCallback<String> callback) {
        String actionName = "ReadJourneyLocation";
       JSONArray jsonArray = new JSONArray();
        /* Map<Long, Long> dateMap = DateUtil.converToDaily(Long.valueOf(startTime), Long.valueOf(endTime));
        for (Map.Entry date : dateMap.entrySet()) {
            String start = String.valueOf(date.getKey());
            String end = String.valueOf(date.getValue());*/
            JSONObject obj = new JSONObject();
            try {
                obj.put(RequestKey.TBOX_ID, tboxid);
                obj.put(RequestKey.STROKE_START, startTime);
                obj.put(RequestKey.STROKE_END, endTime);
                jsonArray.put(obj);
            } catch (Exception e) {

            }
       // }
        CallServer.getRequestInstance().post(context,NetConstant.getRequestUrl(actionName),what,jsonArray.toString(),callback);
    }
}
